package com.wenheng.config;

import com.wenheng.Mapper.EasybuyUserMapper;
import com.wenheng.vo.entity.EasybuyUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Auther: 文恒
 * @Date: 2021/3/17 - 03 - 17 -15:52
 * @Description:com.wenheng.config
 * @version:1.0
 */
public class UserDetils extends AuthorizingRealm{
    @Autowired
    private EasybuyUserMapper easybuyUserMapper;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        EasybuyUser easybuyUser = easybuyUserMapper.findUser(usernamePasswordToken.getUsername());
        if(easybuyUser!=null){
            if(easybuyUser.getPassword().equals(usernamePasswordToken.getPassword())){
                subject.getSession().setAttribute("login",easybuyUser);
            }
        }
        return new SimpleAuthenticationInfo(easybuyUser,easybuyUser.getPassword(),"");
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
